/*
 * @Author: nexox 2685853507@qq.com
 * @Date: 2024-11-26 11:35:11
 * @LastEditors: nexox 2685853507@qq.com
 * @LastEditTime: 2024-11-26 20:24:02
 * @FilePath: \pytorch_workplace\matrix\w12\kh4.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stddef.h>
#include <stdio.h>
#include <string.h>

char *my_strcpy(char *dest, const char *src) {
    int len = strlen(src);
    for (int i = 0; i < len; i++)
        dest[i] = src[i];
    return dest;
}

char *my_strncpy(char *dest, const char *src, size_t n) {
    int len = n;
    for (int i = 0; i < len; i++)
        dest[i] = src[i];
    return dest;
}

size_t my_strlen(const char *str) {
    size_t len = 0;
    while (str[len] != '\0')
        len++;
    return len;
}

int my_strcmp(const char *str1, const char *str2) {
    int l1 = strlen(str1), l2 = strlen(str2);
    for (int i = 0; i < ((l1 < l2) ? l1 : l2); i++) {
        if (str1[i] != str2[i]) {
            return (str1[i] < str2[i]) ? -1 : 1;
        }
    }
    return (l1 == l2) ? 0 : ((l1 > l2) ? 1 : -1);
}

int my_strncmp(const char *str1, const char *str2, size_t n) {
    int l1 = strlen(str1), l2 = strlen(str2);
    for (size_t i = 0; i < n; i++) {
        if (str1[i] != str2[i]) {
            return (str1[i] < str2[i]) ? -1 : 1;
        }
    }
    return (l1 == l2) ? 0 : ((l1 > l2) ? 1 : -1);
}

char *my_strcat(char *dest, const char *src) {
    int len = strlen(src), l = strlen(dest);
    for (int i = 0; i < len; i++) {
        dest[i + l] = src[i];
    }
    dest[len + l] = '\0';
    return dest;
}

char *my_strchr(const char *str, int c) {
    while (*str != '\0') {
        if (*str == (char)c) return (char *)str;
        str++;
    }
    if (c == '\0') return (char *)str;
    return NULL;
}

char *my_strstr(const char *s1, const char *s2) {
    int len1 = strlen(s1), len2 = strlen(s2);
    for (int i = 0; i <= len1 - len2; i++) {
        int isSub = 1;
        for (int j = 0; j < len2; j++) {
            if (s1[i + j] != s2[j]) {
                isSub = 0;
                break;
            }
        }
        if (isSub) return (char *)(s1 + i);
    }
    return NULL;
}

int my_atoi(const char *str) {
    int a = 0;
    int b = 1;
    while (*str != '\0') {
        if (*str == '-') {
            b = -1;
            str++;
            continue;
        }
        a *= 10;
        a += (*str) - '0';
        str++;
    }
    return a * b;
}

double my_atof(const char *str) {
    double d = 0;
    double t = 0;
    double p = 1;
    while (*str != '\0') {
        if (*str == '-') {
            p = -1;
            str++;
            continue;
        }
        t *= 10;
        if (*str == '.') {
            t = 1;
        } else {
            d *= (t == 0) ? 10 : 1;
            d += (double)(*str - '0') / (double)((t == 0) ? 1 : t);
        }
        str++;
    }
    return d * p;
}
